package com.hans.cloud.helloserver.controller;


import com.hans.cloud.api.utils.R;
import com.hans.cloud.common.utils.PageUtils;
import com.hans.cloud.common.utils.Query;
import com.hans.cloud.helloserver.domain.UserDO;
import com.hans.cloud.helloserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/user")
public class UserController {
    @Value("${server.port}")
    private int port;

    @Autowired
    UserService userService;

    @GetMapping
    public String hello(String name) {
        return "Hello "+name+"! (server port + " + port + "). ";
    }

    /**
     * 根据用户id获取用户
     * @param id
     * @return
     */
    @GetMapping("/get")
    R get(@RequestParam(value = "id", required = false, defaultValue = "1") Long id ){
        UserDO user=new UserDO();
        user.setUserId(id);
        user.setUsername("hans");
        user.setName("韩宏武哥");
        return R.ok().put("data",user);
    }

    @GetMapping("/list")
    R listByPage(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        int total = userService.count(query);
        List<UserDO> users=userService.list(query);
        PageUtils pageUtil = new PageUtils(users, total);
        return R.ok().put("page",pageUtil);
    }

}
